package com.central.apps.handler;

import com.central.app.service.HandlerService;
import com.central.apps.dao.comparequalityprice.CompareQualityPriceDao;
import com.central.apps.handler.base.LvBaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author mengweitao
 * @Date 2023/2/1/001 13:42
 */
@Slf4j
@Component
public class CompareQualityPriceHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private CompareQualityPriceDao compareQualityPriceDao;

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("compapriceApp".equals(app)) {
            if ("gq_isofferss".equals(objName)) {
                //子表保存后，算分数
//                String isofferssid = MapUtils.getString(data, "isofferssid");//采购计划子表id
//                List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_isofferss where isofferssid = '" + isofferssid + "'");
//
//                if(CollectionUtils.isNotEmpty(bySql)){
//                    for (Map<String, Object> map : bySql) {
                String compapripplineid = MapUtils.getString(data, "compapripplineid");//采购计划明细行id
                //报价排序分数
                Map<String, Map<String, Object>> unitcost = unitcost(compapripplineid);
                //供货周期（天）排序分数
                Map<String, Map<String, Object>> supplyperiod = supplyperiod(compapripplineid);
                //付款周期(天) 排序分数
                Map<String, Map<String, Object>> paymentperiod = paymentperiod(compapripplineid);
                //质保期(月)  排序分数
                Map<String, Map<String, Object>> warrantyperiod = warrantyperiod(compapripplineid);
                //供应商等级  排序分数

                //到货合格率  排序分数
                Map<String, Map<String, Object>> passrate = passrate(compapripplineid);
                //到货及时率  排序分数
                Map<String, Map<String, Object>> promptness = promptness(compapripplineid);
                //交易次数  排序分数
                Map<String, Map<String, Object>> numtransac = numtransac(compapripplineid);

                //计算分数
                //这条采购计划明细下的所有的供应商报价信息
                List<Map<String, Object>> bySql = handlerService.findBySql("select id from gq_isofferss where compapripplineid = '" + compapripplineid + "'");

                calculatesScore(unitcost,supplyperiod,paymentperiod,warrantyperiod,passrate,promptness,numtransac,bySql);
//                    }
//                }
            } else if("gq_compaprice".equals(objName) && !isUpdate) {
                //主表保存，将主表选择的采购计划编号中的明细行插到子表中
                insertcompaprippline(data);
            }
        }
        super.afterSave(app, objName, data, isUpdate);
    }

    public Map<String,Map<String,Object>> unitcost(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreByUnitcostParam = compareQualityPriceDao.findScoreByUnitcostParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreByUnitcostParam)){
            for (Map<String, Object> map : scoreByUnitcostParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("unitcost",MapUtils.getString(map,"unitcost"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public Map<String,Map<String,Object>> supplyperiod(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreBySupplyperiodParam = compareQualityPriceDao.findScoreBySupplyperiodParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreBySupplyperiodParam)){
            for (Map<String, Object> map : scoreBySupplyperiodParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("supplyperiod",MapUtils.getString(map,"supplyperiod"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public Map<String,Map<String,Object>> paymentperiod(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreByPaymentperiodParam = compareQualityPriceDao.findScoreByPaymentperiodParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreByPaymentperiodParam)){
            for (Map<String, Object> map : scoreByPaymentperiodParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("paymentperiod",MapUtils.getString(map,"paymentperiod"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public Map<String,Map<String,Object>> warrantyperiod(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreByWarrantyperiodParam = compareQualityPriceDao.findScoreByWarrantyperiodParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreByWarrantyperiodParam)){
            for (Map<String, Object> map : scoreByWarrantyperiodParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("warrantyperiod",MapUtils.getString(map,"warrantyperiod"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public Map<String,Map<String,Object>> passrate(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreByPassrateParam = compareQualityPriceDao.findScoreByPassrateParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreByPassrateParam)){
            for (Map<String, Object> map : scoreByPassrateParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("passrate",MapUtils.getString(map,"passrate"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public Map<String,Map<String,Object>> promptness(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreByPromptnessParam = compareQualityPriceDao.findScoreByPromptnessParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreByPromptnessParam)){
            for (Map<String, Object> map : scoreByPromptnessParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("passrate",MapUtils.getString(map,"passrate"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public Map<String,Map<String,Object>> numtransac(String compapripplineid){
        Map resultMap = new HashMap<String,Map<String, Object>>();
        List<Map<String, Object>> scoreByNumtransacParam = compareQualityPriceDao.findScoreByNumtransacParam(compapripplineid);
        if(CollectionUtils.isNotEmpty(scoreByNumtransacParam)){
            for (Map<String, Object> map : scoreByNumtransacParam) {
                Map<String, Object> map1 = new HashMap<>();
                map1.put("numtransac",MapUtils.getString(map,"numtransac"));
                map1.put("score",MapUtils.getString(map,"score"));
                resultMap.put(MapUtils.getString(map,"id"),map1);
            }
        }
        return resultMap;
    }

    public void calculatesScore(Map<String,Map<String,Object>> unitcost,
                                Map<String,Map<String,Object>> supplyperiod,
                                Map<String,Map<String,Object>> paymentperiod,
                                Map<String,Map<String,Object>> warrantyperiod,
                                Map<String,Map<String,Object>> passrate,
                                Map<String,Map<String,Object>> promptness,
                                Map<String,Map<String,Object>> numtransac,
                                List<Map<String, Object>> bySql){
        if(CollectionUtils.isNotEmpty(bySql)){
            for (Map<String, Object> map : bySql) {
                String id = MapUtils.getString(map, "id");//报价供应商id
                Integer unitcostNew = MapUtils.getInteger(unitcost.get(id), "score");
                Integer supplyperiodNew = MapUtils.getInteger(MapUtils.getMap(supplyperiod,id), "score");
                Integer paymentperiodNew = MapUtils.getInteger(MapUtils.getMap(paymentperiod,id), "score");
                Integer warrantyperiodNew = MapUtils.getInteger(MapUtils.getMap(warrantyperiod,id), "score");
                Integer passrateNew = MapUtils.getInteger(MapUtils.getMap(passrate,id), "score");
                Integer promptnessNew = MapUtils.getInteger(MapUtils.getMap(promptness,id), "score");
                Integer numtransacNew = MapUtils.getInteger(MapUtils.getMap(numtransac,id), "score");
                //算供应商等级的分数
                List<Map<String, Object>> bySql1 = handlerService.findBySql("select supliergrade from gq_isofferss where id = '" + id + "'");
                int supliergradescore = 0;
                if(CollectionUtils.isNotEmpty(bySql1)){
                    Map<String, Object> map1 = bySql1.get(0);
                    String supliergrade = MapUtils.getString(map1, "supliergrade");
                    switch (supliergrade){
                        case "优选":
                            supliergradescore = 10;
                            break;
                        case "可选":
                            supliergradescore = 9;
                            break;
                        case "慎选":
                            supliergradescore = 8;
                            break;
                        case "试用":
                            supliergradescore = 7;
                            break;
                        default:
                            break;
                    }

                }
                int scoreNew = unitcostNew + supplyperiodNew + paymentperiodNew + warrantyperiodNew + passrateNew + promptnessNew + numtransacNew;
                BigDecimal scoreNewBig = new BigDecimal(String.valueOf(scoreNew));
                BigDecimal supliergradescoreBig = new BigDecimal(String.valueOf(supliergradescore));
                BigDecimal newBig = scoreNewBig.add(supliergradescoreBig);
                BigDecimal divide = newBig.divide(new BigDecimal("10"));
                handlerService.findBySql("update gq_isofferss set assessment = '" + divide.toString()+ "' where id ='" + id + "'");
            }
        }
    }

    public void insertcompaprippline(Map<String,Object> data) throws Exception {
        String ppnum = MapUtils.getString(data, "ppnum");
        String id = MapUtils.getString(data, "id");
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> bySql = handlerService.findBySql("select * from gq_ppline where ppid in (select id from gq_purchaseplan where ppnum = '" + ppnum + "')");
        if(CollectionUtils.isNotEmpty(bySql)){
            for (Map<String, Object> stringObjectMap : bySql) {
                stringObjectMap.put("compapriceid",id);
            }
        }
        handlerService.batchInsert("compapriceApp","gq_compaprippline",bySql);
    }



}
